//
//  YYJLMessageController.m
//  YuanyangJinlou
//
//  Created by mac004 on 2017/1/3.
//  Copyright © 2017年 suli19910309@qq.com. All rights reserved.
//

#import "YYJLMessageController.h"
#import "YYJLMessageDetailController.h"
@interface YYJLMessageController ()<ChangeViewDelegate,UIScrollViewDelegate>
{
    ChangeView      *_tabbarView;      //分栏
    NSInteger                               selectTabbarIndex; //分栏选中
    NSMutableArray                          *_ViewCtrlArr;
    UIScrollView                            *_scrollView;
}
@end

@implementation YYJLMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    self.view.backgroundColor=UIColorFromHex(0xf7f7f7);
    [self setTitleView:@"消息"];
    [self setBackItem];
    [self setRightBarItems];
    [self creatUI];
    [self addController];
     
}
-(void)setRightBarItems{
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(edit)];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:HSGBW3 size:15], NSFontAttributeName, nil] forState:UIControlStateNormal];
    [self.navigationItem.rightBarButtonItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15], NSFontAttributeName, nil] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem.tintColor=UIColorFromHex(0x333333);

    
}

-(void)creatUI{
    _ViewCtrlArr = [[NSMutableArray alloc]initWithObjects:@"最新主播秀",@"最新宝贝秀", nil];
    _tabbarView = [[ChangeView alloc]initWithFrame:CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, 80*PER_HEIGHT) withCount:_ViewCtrlArr withbgColor:BLACK_COLOR withSelectColor:TINK_COLOR];
    _tabbarView.delegate = self;
    [self.view addSubview:_tabbarView];
    
    //全局滚动
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _tabbarView.bottom+10*PER_HEIGHT, SCREEN_WIDTH, self.view.height-_tabbarView.bottom)];
    _scrollView.backgroundColor = [UIColor clearColor];
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.bounces = YES;
    [self.view addSubview:_scrollView];
    _scrollView.contentSize = CGSizeMake(_scrollView.width*_ViewCtrlArr.count, _scrollView.height);
    

}
//添加子控件
- (void)addController
{
    for (int i=0; i<_ViewCtrlArr.count; i++) {
        YYJLMessageDetailController *show = [[YYJLMessageDetailController alloc]init];
        if (i==0) {
            show.type = PersonalMessage;
        }
        else if (i==1) {
            show.type = SystemMessage;
        }
        
        [self addChildViewController:show];
        
        if (i == 0) {
            show.view.frame = CGRectMake(0, 0, _scrollView.width, _scrollView.height);
            [_scrollView addSubview:show.view];
        }
    }
}
- (void)onSelectItemForView:(ChangeView *)view withIndex:(NSInteger)selectIndex
{
    
     YYJLMessageDetailController *show = [[YYJLMessageDetailController alloc]init];
    
    if (selectIndex==0) {
        show.type=PersonalMessage;
    }else{
        show.type=SystemMessage;
    }

    NSLog(@"tttt==%u",show.type);
//    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
//    if (index != selectIndex) {
//        selectTabbarIndex = selectIndex;
//         YYJLMessageDetailController *show = [[YYJLMessageDetailController alloc]init];
//        
//        if (selectIndex==0) {
//            show.type=PersonalMessage;
//        }else{
//            show.type=SystemMessage;
//        }
//       // [_scrollView setContentOffset:CGPointMake(selectIndex*_scrollView.width, 0) animated:YES];
//      
//        
//
//    }
}
#pragma mark - 滚动
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    if (index != selectTabbarIndex) {
        selectTabbarIndex = index;
        [_tabbarView onUPdateUIForSelectIndex:index];
        
        [self scrollViewDidEndScrollingAnimation:_scrollView];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    NSInteger index = _scrollView.contentOffset.x / _scrollView.width;
    YYJLMessageDetailController *show = self.childViewControllers[index];
    if (show.view.superview) {
        return;
    }
    show.view.frame = scrollView.bounds;
    [_scrollView addSubview:show.view];
}

-(void)edit{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
